import { Component, OnInit, Injector, ViewChild, Input, ViewEncapsulation, Output, EventEmitter } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/table';
import { Paginator, LazyLoadEvent } from 'primeng/primeng';
import {
    ListDtoExtensionsItem,
    StockType,
    Stock_AuditListDto,
    StockZT_AuditServiceProxy,
    BillType
} from '@shared/service-proxies/stock-service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
@Component({
    templateUrl: './audit-log-modal.component.html',
    animations: [appModuleAnimation()],
    selector: 'auditlog',
    encapsulation: ViewEncapsulation.None
})
export class AuditLogComponent extends AppComponentBase implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('createOrEditModal') modal: ModalDirective;
    _type: StockType;
    _billtype:BillType;
    _processid;
    closed = false;
    isShown = false;
    @Input() set stocktype(val) {
        if (val) {
            this._type = val;
        }
    }
    @Input() set billtype(val) {
        if (val) {
            this._billtype = val;
        }
    }
    // @Input() set processid(val) {
    //     if (val) {
    //         this._processid = val;
    //     }
    // }
    @Output() modalSelect = new EventEmitter<Stock_AuditListDto>();
    anyncing = false;
    f_SupplierName;
    f_SupplierShortName;
    selectedValue: Stock_AuditListDto;
    extensions: Array<ListDtoExtensionsItem>;
    constructor(injector: Injector, private _supplierServiceProxy: StockZT_AuditServiceProxy) {
        super(injector);
    }

    ngOnInit() {
    }
    rowSelect() {
        this.modalSelect.emit(this.selectedValue);
    }
    show(processid): void {
        this.closed = false;
        this._processid =processid;
        this.modal.show();
        this.getBaseField();
    }

    shown(): void {
        this.isShown = true;
        this.getBaseField(null);
    }
    close(): void {
        this.closed = true;
        this.modal.hide();
    }

    onHidden(event) {
        this.closed = true;
    }
    getBaseField(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event) || event.first == undefined) {
            this.paginator.changePage(0);

            return;
        }
        this.primengTableHelper.showLoadingIndicator();
        this._supplierServiceProxy
            .getAuditListExtension(
               this._type,
                this._billtype,
                this._processid,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.extensions = result.items;
                this.primengTableHelper.totalRecordsCount = result.dtos.totalCount;
                this.primengTableHelper.records = result.dtos.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
}
